package com.alipay.app.groupmeal.standard.ui.c;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.KeyEvent;
import android.widget.ImageView;
import android.widget.TextView;

import com.alipay.app.groupmeal.standard.R;
import com.alipay.app.groupmeal.standard.base.BaseMVCActivity;
import com.alipay.app.groupmeal.standard.constant.ConfigConstant;
import com.alipay.app.groupmeal.standard.http.bean.resp.Result;
import com.alipay.app.groupmeal.standard.http.bean.resp.ShopMachine;
import com.alipay.app.groupmeal.standard.ui.b.BindDevicePre;
import com.alipay.app.groupmeal.standard.ui.b.util.SysCenterHttpUtil;
import com.alipay.iot.sdk.utils.SystemUtils;

import cn.bingoogolapple.qrcode.zxing.QRCodeEncoder;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.internal.observers.BlockingBaseObserver;

/**
 * 绑定页面
 */
public class BindActivity extends BaseMVCActivity {

    TextView deviceSn;

    private ImageView mIvQrcode;

    private BindDevicePre bindDevicePre;

    public static void startAct(Context context) {
        Intent intent = new Intent(context, BindActivity.class);
        context.startActivity(intent);
    }

    @Override
    public boolean onKeyEventBack(KeyEvent event) {
        finish();
        return super.onKeyEventBack(event);
    }

    @Override
    protected int getLayoutId() {
        switch (ConfigConstant.displayType) {
            case 1:
                return R.layout.view_bind_device;
            default:
                return R.layout.activity_bind_device;
        }
    }

    @Override
    protected void initView() {
        mIvQrcode = findViewById(R.id.iv_qrcode);
        deviceSn = findViewById(R.id.deviceSn);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void dissmissPresnetation() {
        if (bindDevicePre != null) {
            bindDevicePre.dismiss();
        }
    }

    @Override
    public void onBusiness() {
        super.onBusiness();
        SharedPreferences userInfo = getSharedPreferences("sysCost", MODE_PRIVATE);
        SharedPreferences.Editor editor = userInfo.edit();
        new SysCenterHttpUtil(mActivity).intDevice(new BlockingBaseObserver<Result<ShopMachine>>() {
            @Override
            public void onNext(@NonNull Result<ShopMachine> stringResult) {
                if (stringResult.getResult() != null) {
                    //保存设置
                    ShopMachine shopMachine = stringResult.getResult();
                    editor.putString("api", shopMachine.getBusinessUrl());
                    editor.putString("isvName", shopMachine.getIsvName());
                    editor.putString("partnerId", shopMachine.getPartnerId());
                    editor.putString("merchantId", shopMachine.getMerchantId());
                    editor.putString("merchantName", shopMachine.getMerchantName());
                    editor.putString("logicGroupId", shopMachine.getLogicGroupId());
                    editor.putString("abcpAppid", shopMachine.getAbcpAppid());
                    editor.putString("abcpAppVersion", shopMachine.getAbcpAppVersion());
                    editor.putString("devPsk", shopMachine.getMqDevPSK());
                    editor.putString("tenantId", shopMachine.getTenantId());
                    editor.putBoolean("isTenant", shopMachine.getTenant()==null?false:shopMachine.getTenant());
                    editor.commit();
                    System.exit(0);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {

            }
        });


        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                //更新云售卖小程序appid
                mIvQrcode.setImageBitmap(QRCodeEncoder.syncEncodeQRCode(SystemUtils.getSerialNumber(), 300));
                bindDevicePre.setQr(SystemUtils.getSerialNumber());
            }
        });
        deviceSn.setText("SN:" + SystemUtils.getSerialNumber());
    }

    @Override
    public void initPresentation() {
        super.initPresentation();
        if (mDisplay != null) {
            bindDevicePre = new BindDevicePre(BindActivity.this, mDisplay);
            registerPresentation(bindDevicePre);
            showPresentation(bindDevicePre);
            bindDevicePre.show();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


}
